package com.mejlisse.gwt.ui;

import com.google.gwt.app.place.Activity;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.ProxyPlace;
import com.mejlisse.gwt.request.NewsRecord;
import com.mejlisse.gwt.shared.Category;
import com.mejlisse.gwt.request.ApplicationRequestFactory;
import com.mejlisse.gwt.ui.NewsDetailsActivity;
import com.mejlisse.gwt.ui.NewsEditActivity;

/**
 * Maps {@link ProxyPlace} instances to the {@link Activity} to run.
 */
public class NewsActivitiesMapper {
	private final ApplicationRequestFactory requests;
	private final PlaceController placeController;

	public NewsActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
		this.requests = requests;
		this.placeController = placeController;
	}

	public Activity getActivity(ProxyPlace place) {
		switch (place.getOperation()) {
			case DETAILS:
				return new NewsDetailsActivity((NewsRecord) place.getProxy(), requests, placeController);
			case EDIT:
				return new NewsEditActivity((NewsRecord) place.getProxy(), requests, placeController, false);
			case CREATE:
      			return new NewsEditActivity((NewsRecord) place.getProxy(), requests, placeController, true);
		}

		throw new IllegalArgumentException("Unknown operation " + place.getOperation());
	}
}
